<?php
class ConfimgAction extends PublicAction {
	public function index(){
	  $isLogin = session(C('ADMIN_AUTH_KEY'));
	  if(!$isLogin) $this->redirect('User/login');
	  $cimg=M('Confimg');
	  $list = $cimg->order('ciid desc')->select();
	  $this->assign("list",$list);
	  $this->display();
	}
	
	public function reg(){
	  $isLogin = session(C('ADMIN_AUTH_KEY'));
	  if(!$isLogin) $this->redirect('User/login');
	  $cimg=M('Confimg');
	  $map['citype'] = 0;
	  $list = $cimg->where($map)->order('ciid desc')->select();
	  $this->assign("list",$list);
	  $this->display();
	}
	 public function addtopic(){
	   $isLogin = session(C('ADMIN_AUTH_KEY'));
	   if(!$isLogin) $this->redirect('User/login');
	   if($_POST){
	   	 import("ORG.Util.UploadFile");
	        //导入上传类
	        $upload = new UploadFile();
	        //设置上传文件大小
	        $upload->maxSize = 3292200;
	        //设置上传文件规则
	        $upload->saveRule = uniqid;
	        //设置上传文件类型
	        $upload->allowExts = explode(',', 'bmp,jpg,gif,png,jpeg');
	        //设置附件上传目录
	        $upload->thumb = true;
		    $upload->imageClassPath = 'ORG.Util.Image';
	        $upload->thumbPrefix = 'm_';
	        $upload->thumbMaxWidth = 203;
		    $upload->thumbMaxHeight = 69;
	        $upload->savePath = './Public/Admin/Topic/';
	        if (!$upload->upload($_FILES)) {
	            //捕获上传异常
	            $this->error($upload->getErrorMsg());
	        } else {
	            //取得成功上传的文件信息
	            $uploadList = $upload->getUploadFileInfo();
	        }
	   	     $_POST['topimg']=$uploadList[0]['savename'];
	       // print_r($uploadList);exit();
	        $cond=$_POST;
		   	$cond['_column'] = array('toptitle','topimg');
			foreach ($cond['_column'] as $one) $data[$one] = trim($cond[$one]);
			$topic=D('Topic');
			if($topic->create($data)){
				if($topic->add()!=false){
				  $this->redirect('Topic/index');
				}
			}else{
				  $this->assign('erorsta','1');
				  $this->assign('error',$topic->getError());
			}
	    }
	   $this->display();
	   }
   
   public function edit(){
   $isLogin = session(C('ADMIN_AUTH_KEY'));
   if(!$isLogin) $this->redirect('User/login');
    $fid['topid']=$_GET['topid'];
    $model=D('Topic');
    $data=$model->where($fid)->find();
    $this->assign('data',$data);
    if($_POST){
    	if(!empty($_FILES['topimg']['name'])){
    	import("ORG.Util.UploadFile");
        //导入上传类
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //设置上传文件规则
        $upload->saveRule = uniqid;
        //设置上传文件类型
        $upload->allowExts = explode(',', 'bmp,jpg,gif,png,jpeg');
        $upload->thumb = true;
	    $upload->imageClassPath = 'ORG.Util.Image';
        $upload->thumbPrefix = 'm_';
        $upload->thumbMaxWidth = 203;
	    $upload->thumbMaxHeight = 69;
        //设置附件上传目录
        $upload->savePath = './Public/Admin/Topic/';
        if (!$upload->upload($_FILES)) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
        	unlink('./Public/Admin/Topic/'.$data['topimg']);
        	unlink('./Public/Admin/Topic/m_'.$data['topimg']);
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
        }
   	     $_POST['topimg']=$uploadList[0]['savename'];
    	}
    	$info=$_POST;
	    $info['topid']=$_GET['topid'];
	    if($model->create($info)){
	   	 $model->save();
	   	 $this->redirect('Topic/index');
	   	}else{
				  $this->assign('erorsta','1');
				  $this->assign('error',$model->getError());
			}
    }

    $this->display();
   }
   
  public function move(){
  $isLogin = session(C('ADMIN_AUTH_KEY'));
  if(!$isLogin) $this->redirect('User/login');
    if($_GET['topid']){
	    $fid['topid']=$_GET['topid'];
	    $model=D('Topic');
	    $name=$model->field('topimg')->where($fid)->find();
	    if($name){
		    $model->where($fid)->delete();
		    unlink('./Public/Admin/Topic/'.$name['topimg']);
		    unlink('./Public/Admin/Topic/m_'.$name['topimg']);
		    $this->redirect('Topic/index');
	    }else{
			  $this->redirect('Topic/index');
		}
    }else{
		$this->redirect('Topic/index');
    }
    
  }
   
	//禁用
	public function forbidden()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$topid = $_POST['topid'];
		
		if($topid)
		{
			$map['topid'] = $topid;
			//$map["status"] = 1;
			$data["status"] = 0;
			$res = M('Topic')->where($map)->save($data);
			if($res) echo 1;
			else echo 0;
		}

		else echo 0;
	}
	
	//启用
	public function enable()
	{
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$topid = $_POST['topid'];
		
		if($topid)
		{
			$map['topid'] = $topid;
			//$map["status"] = 0;
			$data["status"] = 1;
			$res = M('Topic')->where($map)->save($data);
			if($res) echo 1;
			else echo 0;
		}

		else echo 0;
	}
 }